package com.bsoft.gol.hcs.satisfaction.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import org.hibernate.SQLQuery;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import com.bsoft.gol.hcs.satisfaction.bean.qo.DictionaryQo;
import com.bsoft.gol.hcs.satisfaction.bean.vo.DictionaryVo;
import com.bsoft.gol.hcs.satisfaction.entity.SurgicalType;
import com.bsoft.gol.hcs.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by ChenJij on 2020/12/15.
 */
public abstract class SurgicalTypeDAO extends HibernateSupportDelegateDAO<SurgicalType> {
    public SurgicalTypeDAO() {
        this.setKeyField("surgicalTypeId");
    }

    @DAOMethod(sql = " from SurgicalType where orgId =:orgId ")
    public abstract List<SurgicalType> querySurgicalTypeList(@DAOParam("orgId") String orgId);

    public List<DictionaryVo> findSurgicalTypeList(final DictionaryQo dictionaryQo) {
        HibernateStatelessResultAction<List<DictionaryVo>> action = new AbstractHibernateStatelessResultAction<List<DictionaryVo>>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append(" select surgical_type_code as dicCode, surgical_type_name as dicName from surgical_type where 1=1 and delete_flag='0' ");
                if(CommonUtils.isNotEmpty(dictionaryQo.getOrganizationId())){
                    stringBuffer.append(" and org_id=:orgId ");
                }
                if(CommonUtils.isNotEmpty(dictionaryQo.getPinyinCode())){
                    stringBuffer.append(" and pinyin_code like:pinyinCode ");
                }
                SQLQuery sqlQuery = ss.createSQLQuery(stringBuffer.toString());
                if(CommonUtils.isNotEmpty(dictionaryQo.getOrganizationId())){
                    sqlQuery.setParameter("orgId",dictionaryQo.getOrgId());
                }
                if(CommonUtils.isNotEmpty(dictionaryQo.getPinyinCode())){
                    sqlQuery.setParameter("pinyinCode",dictionaryQo.getPinyinCode()+"%");
                }
                int first = dictionaryQo.getPageNo() > 0 ? (dictionaryQo.getPageNo() - 1) * dictionaryQo.getPageSize() : 0;
                sqlQuery.setFirstResult(first);
                sqlQuery.setMaxResults(dictionaryQo.getPageSize());
                sqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<DictionaryVo> dictionaryVoList = new ArrayList<DictionaryVo>();
                List<Map> list = sqlQuery.list();
                if (list.size() > 0) {
                    for (Map map : list) {
                        dictionaryVoList.add(ConversionUtils.convert(map, DictionaryVo.class));
                    }
                    setResult(dictionaryVoList);
                } else {
                    setResult(null);
                }
                setResult(sqlQuery.list());
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }




}